package com.skyline.jfs.cluster.controller;

import java.io.IOException;
import java.rmi.NotBoundException;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.skyline.jfs.cluster.model.JFSNodeConfigProper;
import com.skyline.jfs.cluster.service.JFSNodeService;
import com.skyline.jfs.cluster.service.LocalFSInfoSerivce;
import com.skyline.jfs.common.config.Config;
import com.skyline.jfs.storage.bean.StorerProxy;

@Controller
@RequestMapping(value = "/cluster/")
public class JFSNodeController {

	@Autowired
	private JFSNodeService jfsNodeService;

	@Autowired
	private LocalFSInfoSerivce localFSInfoSerivce;

	@RequestMapping(value = "/getLocalStatus.html", method = RequestMethod.GET)
	public String getLocalStatus(HttpServletRequest request) {
		localFSInfoSerivce.initalLocalFSInfo();
		request.setAttribute("jfsNodeConfigProper", JFSNodeConfigProper.getProper());
		return "cluster/getLocalStatus";
	}

	@RequestMapping(value = "/getClusterStatus.html", method = RequestMethod.GET)
	public String getClusterStatus(HttpServletRequest request) {
		request.setAttribute("clusterNodes", Config.getClusterNodes());
		return "cluster/getClusterStatus";
	}

	@RequestMapping(value = "/configThisNode.html", method = RequestMethod.GET)
	public String configThisNodeRequest(HttpServletRequest request) {
		localFSInfoSerivce.initalLocalFSInfo();
		request.setAttribute("jfsNodeConfigProper", JFSNodeConfigProper.getProper());
		return "cluster/configThisNode";
	}

	@RequestMapping(value = "/configThisNode.html", method = RequestMethod.POST)
	public String configThisNodeExecute(HttpServletRequest request, String hostname, String ip, Long adminJobNum,
			Integer loadbalacingFactor, String storageDirectory, String description) {
		jfsNodeService.configThisNode(hostname, ip, adminJobNum, loadbalacingFactor, storageDirectory, description);
		return "redirect:getLocalStatus.html";
	}

	// @RequestMapping(value="/testStore.html" , method=RequestMethod.GET)
	// public String test(){
	// try {
	// storerProxy.store("/media/Data/photography/11/2/216CANON/IMG_0648.JPG",
	// 1, "jpg");
	// } catch (NotBoundException e) {
	// // TODO Auto-generated catch block
	// e.printStackTrace();
	// } catch (IOException e) {
	// // TODO Auto-generated catch block
	// e.printStackTrace();
	// }
	// return "test";
	// }
}
